package com.itheima.demo;

import com.itheima.domain.Student;

import java.util.Comparator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

public class Demo_TreeMap {
    public static void main(String[] args) {
        TreeMap<Student, String> map = new TreeMap<>(new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return o2.getAge()-o1.getAge();
            }
        });
        Student s1 = new Student("xiaozhang", 23);
        Student s2 = new Student("xiaoli   ", 24);
        Student s3 = new Student("xiaowang ", 25);
        Student s4 = new Student("xiaobai  ", 26);
        Student s5 = new Student("xiaoguo  ", 27);
        Student s6 = new Student("xiaoliu  ", 28);
        map.put(s1, "西安");
        map.put(s2, "咸阳");
        map.put(s3, "渭南");
        map.put(s4, "安康");
        map.put(s5, "商洛");
        map.put(s6, "汉中");
        map.forEach((key, value) -> {
            System.out.println(key + " = " + value);
        });
        System.out.println("=============================");
        //使用第一种方式遍历;
        Set<Student> keys = map.keySet();
        for (Student key : keys) {
            String value = map.get(key);
            System.out.println(key + " = " + value);
        }
        System.out.println("=============================");
        //使用第二种方式遍历;
        Set<Map.Entry<Student, String>> entries = map.entrySet();
        for (Map.Entry<Student, String> entry : entries) {
            Student key = entry.getKey();
            String value = entry.getValue();
            System.out.println(key + " = " + value);
        }
    }
}
